
from multiprocessing import Pool
import re

def test_seq(seq,qua):
    count_AT = len(re.findall(r'[AT]', seq))
    if count_AT >= 35:
        return False,False
    seq = re.search(r"^A*([TGCN][ATGCN]{1,})$",seq[::-1]).group(1)[::-1]
    qua = qua[:len(seq)]
    return seq,qua

def caa(sample):
    path = f"rawdata/{sample}_RC.fastq"

    with open(f"CAAdata/py_{sample}_CAA.fastq","w",encoding="utf8") as output:
        with open(path,"r",encoding="utf8") as f:
            # for i in range(N*4):
            line1 = f.readline()
            line2 = f.readline()
            line3 = f.readline()
            line4 = f.readline()
            while line1:
                seq,qua = test_seq(line2.strip(),line4.strip())
                if seq:
                    output.write(line1)
                    output.write(seq+'\n')
                    output.write(line3)
                    output.write(qua+'\n')
                line1 = f.readline()
                line2 = f.readline()
                line3 = f.readline()
                line4 = f.readline()

if __name__ == "__main__":
    po=Pool(4)
    for sample in (43,44,45,46):
        po.apply_async(caa,(sample,))